package org.ne.banksys.mapper;

import com.github.pagehelper.Page;
import org.apache.ibatis.annotations.*;
import org.ne.banksys.entity.Trade;
import org.ne.banksys.entity.User;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.List;

/**
 * @author wey
 * @date 2025/6/13 下午10:40 06
 **/

@Mapper
public interface TradeMapper {

    @Insert("insert into trade(trade_date,trade_type,card_id,trade_money,remark)" +
            "values (#{tradeDate}, #{tradeType}, #{cardId}, #{tradeMoney}, #{remark})")
    void insert(Trade trade);

    @Select("select * from trade")
    List<Trade> getAll();


    @Delete("delete from trade where trade_id = #{tradeId}")
    void delete(Long tradeId);

    @Update("update trade set remark = #{remark} where trade_id=#{tradeId}")
    void update(Trade trade);

    @Select("select * from trade where trade_id=#{tradeId}")
    Trade get(Long tradeId);


    @Select("select * from trade limit #{pageNum}, #{pageSize}")
    Page<Trade> getPage(@Param("pageNum") Integer pageNum, @Param("pageSize") Integer pageSize);


    @Select("SELECT * FROM trade t WHERE " +
            "(#{startDate} IS NULL OR t.trade_date >= #{startDate}) AND " +
            "(#{endDate} IS NULL OR t.trade_date <= #{endDate})")
    List<Trade> findByTradeDateRange(
            @Param("startDate") Timestamp startDate,
            @Param("endDate") Timestamp endDate
    );

    @Select("SELECT * FROM trade t WHERE " +
            "(#{minMoney} IS NULL OR ABS(t.trade_money) >= #{minMoney}) AND " +
            "(#{maxMoney} IS NULL OR ABS(t.trade_money) <= #{maxMoney})")
    List<Trade> findByTradeMoneyRange(
            @Param("minMoney") BigDecimal minMoney,
            @Param("maxMoney") BigDecimal maxMoney
    );


    @Select("SELECT * FROM trade t WHERE " +
            "(#{minMoney} IS NULL OR ABS(t.trade_money) >= #{minMoney}) AND " +
            "(#{maxMoney} IS NULL OR ABS(t.trade_money) <= #{maxMoney}) AND" +
            "(#{startDate} IS NULL OR t.trade_date >= #{startDate}) AND " +
            "(#{endDate} IS NULL OR t.trade_date <= #{endDate})"


    )

    List<Trade> findByMoneyAndDate(
            @Param("minMoney") BigDecimal minMoney,
            @Param("maxMoney") BigDecimal maxMoney,
            @Param("startDate") Timestamp startDate,
            @Param("endDate") Timestamp endDate);
    }
